<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">报修人员登录</h2>
      <el-form ref="loginForm" :model="loginForm" label-width="100px" class="login-form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" autocomplete="off" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password" autocomplete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login" class="login-button">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: ""
      }
    };
  },
  methods: {
    async login() {
      try {
        console.log(this.loginForm)
        const response = await axios.post("http://localhost:3000/worker/login", this.loginForm);
        const { data } = response;
        this.$message.success(data.message || "登录成功");
        if (data.token) {
          localStorage.setItem("workerToken", data.token);
          this.$router.push("/workerPage/repairManagement"); // 登录后跳转到工作台
        } else {
          this.$message.warning("登录成功但未收到Token");
        }
      } catch (err) {
        let errorMessage = "登录失败";
        if (err.response && err.response.data) {
          errorMessage = err.response.data.message || errorMessage;
        }
        this.$message.error(errorMessage);
      }
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #42a5f5, #7e57c2);
}

.login-card {
  width: 400px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  background-color: #ffffff;
  padding: 20px;
}

.login-title {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  color: #333333;
  margin-bottom: 20px;
}

.login-form {
  padding: 10px 0;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input {
  border-radius: 8px;
  height: 40px;
}

.login-button {
  width: 100%;
  height: 45px;
  border-radius: 22px;
  font-size: 16px;
  font-weight: bold;
  background: linear-gradient(90deg, #42a5f5, #7e57c2);
  color: #ffffff;
  border: none;
  transition: all 0.3s;
}

.login-button:hover {
  background: linear-gradient(90deg, #7e57c2, #42a5f5);
  transform: scale(1.05);
}
</style>
